package com.example.chatweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatweb.entity.BigBase;
import com.example.chatweb.entity.PersonalBase;
import com.example.chatweb.mapper.BigBaseMapper;
import com.example.chatweb.mapper.PersonalBaseMapper;
import com.example.chatweb.service.PersonalBaseService;
import java.math.BigInteger;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonalBaseServiceImpl extends
 ServiceImpl<PersonalBaseMapper, PersonalBase> implements PersonalBaseService {
	
	@Autowired
	private BigBaseMapper bigBaseMapper;
	@Autowired
	private PersonalBaseMapper personalBaseMapper;
	
	
	@Override
	public void copyFromBigToPersonal(Long pdfUuid,Long hubUuid) {
		// 从大库中根据 ID 查询数据
		BigBase bigBase = bigBaseMapper.selectById(pdfUuid);
		
		if (bigBase != null) {
			// 复制数据到个人库中
			PersonalBase personalBase = new PersonalBase();
			personalBase.setPdfUuid(pdfUuid);
			personalBase.setDoi(bigBase.getDoi());
			personalBase.setTitle(bigBase.getTitle());
			personalBase.setKeyword(bigBase.getKeyword());
			personalBase.setAuthor(bigBase.getAuthor());
			personalBase.setAffiliation(bigBase.getAffiliation());
			personalBase.setPublicTime(bigBase.getPublicTime());
			personalBase.setPublicJournal(bigBase.getPublicJournal());
			personalBase.setIsConference(bigBase.getIsConference());
			personalBase.setConferenceName(bigBase.getConferenceName());
			personalBase.setConferenceTime(bigBase.getConferenceTime());
			personalBase.setStaticUrl(bigBase.getStaticUrl());
			personalBase.setHubUuid(hubUuid);
			
			// 插入个人库
			personalBaseMapper.insert(personalBase);
		} else {
			throw new IllegalArgumentException(
			 "No data found in big repository with UUID: " + pdfUuid);
		}
	}
	
	@Override
	public boolean updatePersonalBaseByPrimaryKey(PersonalBase personalBase) {
		// 构建更新条件
		UpdateWrapper<PersonalBase> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("pdf_uuid", personalBase.getPdfUuid())
		 .eq("hub_uuid", personalBase.getHubUuid());
		
		// 执行更新操作
		int updateCount = personalBaseMapper.update(personalBase, updateWrapper);
		
		// 返回更新结果
		return updateCount > 0;
	}
	
	@Override
	public boolean deletePersonalBase(Long pdfUuid, Long hubUuid) {
		// 构建删除条件
		QueryWrapper<PersonalBase> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("pdf_uuid", pdfUuid)
		 .eq("hub_uuid", hubUuid);
		
		// 执行删除操作
		int deleteCount = personalBaseMapper.delete(queryWrapper);
		
		// 返回删除结果
		return deleteCount > 0;
	}
	
	@Override
	public List<PersonalBase> getPdfsByHubUuid(Long hubUuid) {
		QueryWrapper<PersonalBase> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("hub_uuid", hubUuid);
		return personalBaseMapper.selectList(queryWrapper);
	}

	@Override
	public boolean checkIfPdfExists(Long pdfUuid, Long hubUuid) {

		LambdaQueryWrapper<PersonalBase> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PersonalBase::getPdfUuid, pdfUuid)
				.eq(PersonalBase::getHubUuid, hubUuid);

		Long count = personalBaseMapper.selectCount(queryWrapper);

		return count > 0;
	}
}

